package com.logistics.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.logistics.compont.ResponseApi;
import com.logistics.entity.LoginUser;
import com.logistics.entity.StatusEntity;
import com.logistics.pojo.Employee;
import com.logistics.service.EmployeeService;
import com.logistics.util.CodeUtil;
import com.logistics.util.JwtUtil;
import com.logistics.util.SmsUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("sms")
public class SmsController {



    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送短信验证码
     * @param mobile
     * @return
     */
    @GetMapping("/sendcode")
    public ResponseApi sendcode(String mobile){

        //1、生成验证码
        String sysCode = CodeUtil.generator(StatusEntity.CODE_SAR);
        System.out.println("sysCode : " + sysCode);

        //2、存入redis
        stringRedisTemplate.boundHashOps("SYS_CODE").put(mobile,sysCode);
        stringRedisTemplate.expire(mobile,5L, TimeUnit.MINUTES);

        //3、发送短信验证码(优化 mq)
        Map<String,String> map = new HashMap<String,String>();
        map.put(mobile,sysCode);

        //内存优化 避免java持久化占用更多资源
        String smsMap = JSON.toJSONString(map);

        rabbitTemplate.convertAndSend("mq.sms",smsMap);
//        SmsUtil.sendCode(mobile,sysCode);

        return ResponseApi.SUCCESS("短信发送成功。");
    }

    /**
     * 校验验证码
     */
    @GetMapping("/validateCode")
    public ResponseApi validateCode(String mobile,String code){

        //1、从系统中取系统验证码
        String sys_code = (String)stringRedisTemplate.boundHashOps("SYS_CODE").get(mobile);

        //2、校验验证码
        if(ObjectUtils.isEmpty(sys_code)){
            return ResponseApi.FAILED("验证码过期,请重新登录。");
        }

        //2.1 不通过 返回验证码错误
        if(!sys_code.equals(code)){
            return ResponseApi.FAILED("验证码错误。");
        }

        //2.2 通过 根据手机号查询用户信息 生成token 存入redis 赋给验证框架
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",mobile);
        queryWrapper.eq("state", StatusEntity.STATUS);
        Employee employee = employeeService.getOne(queryWrapper);

        //3.1 验证手机号码用户信息
        if(ObjectUtils.isEmpty(employee)){
            return ResponseApi.FAILED("手机号不存在。");
        }

        //4、生成token
        String token = JwtUtil.createJWT(employee.getEid()+"");
        Map<String, String> map = new HashMap<>();
        map.put("token",token);

        //把完整的用户信息存入redis  userid为key   用户信息为value
        LoginUser loginUser = new LoginUser();
        loginUser.setEmployee(employee);
        stringRedisTemplate.boundHashOps("com.logistics:loginUser").put("login:"+employee.getEid(), JSON.toJSONString(loginUser));

        //失效策略
        stringRedisTemplate.expire("loginUser",2, TimeUnit.HOURS);

        //自定义返回值信息
        ResponseApi responseApi = new ResponseApi();
        responseApi.setMessage("短信登录成功");
        responseApi.setData(map);

        //5、删除redis code
        stringRedisTemplate.boundHashOps("SYS_CODE").delete(mobile);

        return responseApi;
    }

}
